/*
 *2011-04-21 created by liugq
 */
package com.jwater.web.action;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.google.gson.Gson;
import com.opensymphony.xwork2.ActionSupport;

public class BaseAction extends ActionSupport {

	private static final long serialVersionUID = -5762558874279426884L;

	private String resultName;
	private String resultValue;

	private int rowCount = 0;
	private int pageSize = 1;
	private int pageCount = 0;
	private int pageIndex = 1;

	private String USER_AGENT = ServletActionContext.getRequest().getHeader("USER-AGENT").toLowerCase();;

	protected static Gson gson = new Gson();

	public String getResultName() {
		return resultName;
	}

	public void setResultName(String resultName) {
		this.resultName = resultName;
	}

	public String getResultValue() {
		return resultValue;
	}

	public void setResultValue(String resultValue) {
		this.resultValue = resultValue;
	}

	public int getRowCount() {
		return rowCount;
	}

	protected void setRowCount(int rowCount) {
		this.rowCount = rowCount;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public int getPageCount() {
		if (rowCount % pageSize == 0)
			pageCount = rowCount / pageSize;
		else
			pageCount = rowCount / pageSize + 1;
		return pageCount;
	}

	/*
	 * protected void setPageCount(int pageCount) { this.pageCount = pageCount;
	 * }
	 */
	public int getPageIndex() {
		return pageIndex;
	}

	public void setPageIndex(int pageIndex) {
		this.pageIndex = pageIndex;
	}

	public String getUSER_AGENT() {
		return USER_AGENT;
	}

	protected void printToJson(Object src) throws IOException {
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("text/json;charset=utf-8");
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter out = response.getWriter();
		String json = gson.toJson(src);
		out.print(json);
		out.flush();
		out.close();
	}

	@Override
	public String execute() throws Exception {
		if (this.resultName != null && !this.resultName.isEmpty())
			return this.resultName;
		else
			return super.execute();
	}
}
